package com.dudu.dao;



import java.sql.Connection;
import java.util.List;

import com.dudu.bean.IMSBilBean;
import com.dudu.model.QueryStatusAccount;

import net.sf.json.JSONObject;


import core.CoreDao;
import core.CoreService;
import core.db.DBConnectionUtil;
/**
 * t_meet_ims_callback
 * @author gaoxs 
 *
 */
public class MeetImsDetailDao {
	/**
	 * 插入一条t_meet_ims_callback信息
	 * @param dao
	 * @param bean
	 * @return
	 */
	public int insert(CoreDao dao, IMSBilBean bean) {

		Connection con = null;
		int result = -1;
		try {
			
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer("INSERT INTO t_meet_ims_callback(session_id, beginTime, endTime, time,fee,caller,callee,ChargeNumber,displayNbr,ability_type,meet_AccessPhone,meet_CreatedTime,meet_ColsedTime,meet_OrganizationId)");
			sql.append(" VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

/*
			CoreService.log.info("sql="+sql.toString()+"------------------------------------");
			CoreService.log.info("value:bean.getSession_id()="+bean.getSession_id());
			CoreService.log.info("value:bean.getBeginTime()="+bean.getBeginTime());
			CoreService.log.info("value:bean.getEndTime()="+bean.getEndTime());
			CoreService.log.info("value:bean.getTime()="+bean.getTime());
			CoreService.log.info("value:bean.getFee()="+bean.getFee());
			CoreService.log.info("value:bean.getCaller()="+bean.getCaller());
			CoreService.log.info("value:bean.getCallee()="+bean.getCallee());
			CoreService.log.info("value:bean.getChargeNumber()="+bean.getChargeNumber());
			CoreService.log.info("value:bean.getDisplayNbr()="+bean.getDisplayNbr());
			CoreService.log.info("value:bean.getAbility_type()="+bean.getAbility_type());
			CoreService.log.info("value:bean.getMeet_AccessPhone()="+bean.getMeet_AccessPhone());
			CoreService.log.info("value:bean.getMeet_CreatedTime()="+bean.getMeet_CreatedTime());
			CoreService.log.info("value:bean.getMeet_ColsedTime()="+bean.getMeet_ColsedTime());
			CoreService.log.info("value:bean.getMeet_OrganizationId()="+bean.getMeet_OrganizationId()+"-------------------------------");
			*/
			List<JSONObject> list = dao.serviceJsonSql(
					con,
					sql.toString(),
					new Object[] {
						bean.getSession_id(),
						bean.getBeginTime(),
						bean.getEndTime(),
						bean.getTime(),
						bean.getFee(),
						bean.getCaller(),
						bean.getCallee(),
						bean.getChargeNumber(),
						bean.getDisplayNbr(),
						bean.getAbility_type(),
						bean.getMeet_AccessPhone(),
						bean.getMeet_CreatedTime(),
						bean.getMeet_ColsedTime(),
						bean.getMeet_OrganizationId()
						
					});

			JSONObject obj = list.get(0);
			result = obj.optInt("result");

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
	
	public IMSBilBean query(CoreDao dao,IMSBilBean record) {

		Connection con = null;
		IMSBilBean result = null;
		
		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer("SELECT id FROM t_meet_ims_callback WHERE  session_id=? and beginTime=? and endTime=? and callee=? and caller=?");
			List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(),new Object[] {
				
				record.getSession_id(),
				record.getBeginTime(),
				record.getEndTime(),
				record.getCallee(),
				record.getCaller()
				
			});
			if (list != null && list.size() > 0) {
				List<IMSBilBean> items = dao.covertToBean(list, IMSBilBean.class);
				result = items.get(0);
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
	
	public int queryDetailCountBySessionId(CoreDao dao,String sessionId) {

		Connection con = null;
		int result = -1;
		
		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer("SELECT count(*) as recordCount FROM t_meet_ims_callback WHERE  session_id='"+sessionId+"' ");
			
			CoreService.log.info("queryDetailCountBySessionId sql="+sql.toString());
			
			List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(),new Object[] {
				
				null
				
			});

			if (list != null && list.size() > 0) {
				result = list.get(0).optInt("recordCount");
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
	
	public QueryStatusAccount queryStatusCountBySessionId(CoreDao dao,String sessionId) {

		Connection con = null;
		QueryStatusAccount result = null;
		
		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer("SELECT count(*) as recordCount,vServiceType  FROM t_call_status " +
					"WHERE  vSessionsId='"+sessionId+"'");
			sql.append(" and vCallState='DISCONNECTED'");
			
			CoreService.log.info("queryStatusCountBySessionId sql="+sql.toString());
			
			List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(),new Object[] {
				
				null
				
			});

			List<QueryStatusAccount> items = dao.covertToBean(list, QueryStatusAccount.class);
			result = items.get(0);

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
	

}
